package com.huluobo.sign.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.TimeUtils
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.sign.SignIntent
import com.huluobo.sign.SignViewModel
import com.huluobo.sign.entity.SignBeanItem
import com.king.ultraswiperefresh.UltraSwipeRefresh
import com.king.ultraswiperefresh.rememberUltraSwipeRefreshState
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 *  Created by LC on 2025/2/7.
 */
@Composable
fun SignPage(navHostController: NavHostController, vm: SignViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<SignBeanItem>()
    }

    val state = rememberUltraSwipeRefreshState()
    var page by remember {
        mutableStateOf(1)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.onError -> {
                        Log.e("TagA", "获取签到数据失败:${it.msg}")
                    }

                    UIState.onLoading -> {}
                    is UIState.onSuccess<*> -> {
                        list.addAll(it.data as Collection<SignBeanItem>)
                    }
                }
            }
        }
        vm.sendIntent(SignIntent.getSigns("1", "10"))
    })

    LaunchedEffect(key1 = state.isRefreshing, block = {
        if (state.isRefreshing) {
            page = 1
            vm.sendIntent(SignIntent.getSigns("$page", "10"))
            state.isRefreshing = false
        }
    })

    LaunchedEffect(key1 = state.isLoading, block = {
        if (state.isLoading) {
            page++
            vm.sendIntent(SignIntent.getSigns("$page", "10"))
            state.isLoading = false
        }
    })

    Scaffold(
        bottomBar = {
            Button(modifier = Modifier
                .padding(10.dp, 10.dp)
                .fillMaxWidth(), onClick = {
                navHostController.navigate(RoutePath.SIGN_COMMIT)
            }) {
                Text(text = "签到")
            }
        }
    ) {
        Column(
            Modifier
                .padding(it)
                .padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            BaseTitle(title = "考勤打卡", navHostController = navHostController)
            Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                val week = TimeUtils.getChineseWeek(System.currentTimeMillis())
                Text(text = "时间$week")
                Text(text = "考勤规则")
            }
            UltraSwipeRefresh(
                state = state,
                onRefresh = { state.isRefreshing = true },
                onLoadMore = { state.isLoading = true }) {
                LazyColumn(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(10.dp),
                    content = {
                        items(list) {
                            Text(
                                text = it.clockTime, modifier = Modifier
                                    .fillMaxWidth()
                                    .background(Color.LightGray)
                                    .padding(20.dp)
                            )
                        }
                    })
            }
        }
    }
}